<template>
  <div class="table-management">
    <h2 style="margin-bottom: 20px;">台桌管理</h2>
    
    <!-- 搜索表单 -->
    <div class="search-form">
      <el-form :model="searchForm" inline>
        <el-form-item label="台桌编号">
          <el-input
            v-model="searchForm.table_no"
            placeholder="请输入台桌编号"
            clearable
            style="width: 200px;"
          />
        </el-form-item>
        
        <el-form-item label="台桌类型">
          <el-select
            v-model="searchForm.table_type"
            placeholder="请选择台桌类型"
            clearable
            style="width: 150px;"
          >
            <el-option label="九球桌" value="nine_ball" />
            <el-option label="斯诺克桌" value="snooker" />
            <el-option label="中式八球" value="chinese_eight" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="台桌状态">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
            style="width: 120px;"
          >
            <el-option label="可用" value="available" />
            <el-option label="占用中" value="occupied" />
            <el-option label="维护中" value="maintenance" />
            <el-option label="已禁用" value="disabled" />
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="getTables">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 操作按钮 -->
    <div class="table-actions">
      <div class="actions-left">
        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon>
          添加台桌
        </el-button>
        <el-button type="success" @click="handleRefresh">
          <el-icon><Refresh /></el-icon>
          刷新
        </el-button>
      </div>
    </div>
    
    <!-- 台桌表格 -->
    <div class="content-card">
      <el-table
        v-loading="loading"
        :data="tables"
        style="width: 100%"
      >
        <el-table-column prop="table_no" label="台桌编号" width="100" />
        <el-table-column prop="table_name" label="台桌名称" min-width="120" />
        
        <el-table-column label="台桌类型" width="100">
          <template #default="scope">
            {{ getTableTypeText(scope.row.table_type) }}
          </template>
        </el-table-column>
        
        <el-table-column label="位置信息" min-width="150">
          <template #default="scope">
            <div v-if="scope.row.area">{{ scope.row.area }}</div>
            <div v-if="scope.row.floor" class="floor-info">
              {{ scope.row.floor }}楼 {{ scope.row.position }}
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="费率信息" min-width="120">
          <template #default="scope">
            <div class="rate-info">
              <div>标准：¥{{ scope.row.hourly_rate }}/时</div>
              <div v-if="scope.row.member_rate">会员：¥{{ scope.row.member_rate }}/时</div>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="当前使用" min-width="120">
          <template #default="scope">
            <div v-if="scope.row.status === 'occupied'">
              <div v-if="scope.row.current_user">用户ID: {{ scope.row.current_user_id }}</div>
              <div v-if="scope.row.occupied_since" class="occupied-time">
                {{ formatTime(scope.row.occupied_since) }}
              </div>
            </div>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column label="设备" width="80">
          <template #default="scope">
            <div class="equipment">
              <el-icon v-if="scope.row.has_lighting" title="有照明"><Light /></el-icon>
              <el-icon v-if="scope.row.has_scoreboard" title="有计分牌"><Monitor /></el-icon>
              <el-icon v-if="scope.row.is_premium" title="VIP台桌" style="color: gold;"><Crown /></el-icon>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="160" fixed="right">
          <template #default="scope">
            <el-button type="text" size="small" @click="viewTable(scope.row)">
              查看
            </el-button>
            <el-button type="text" size="small" @click="editTable(scope.row)">
              编辑
            </el-button>
            <el-dropdown @command="handleTableCommand" trigger="click">
              <el-button type="text" size="small">
                更多<el-icon><ArrowDown /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item :command="{action: 'maintenance', row: scope.row}">
                    设为维护
                  </el-dropdown-item>
                  <el-dropdown-item :command="{action: 'enable', row: scope.row}">
                    设为可用
                  </el-dropdown-item>
                  <el-dropdown-item :command="{action: 'disable', row: scope.row}" divided>
                    禁用台桌
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.per_page"
          :page-sizes="[10, 20, 50]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="getTables"
          @current-change="getTables"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)

// 搜索表单
const searchForm = ref({
  table_no: '',
  table_type: '',
  status: ''
})

// 台桌列表
const tables = ref([])

// 分页
const pagination = ref({
  page: 1,
  per_page: 20,
  total: 0
})

// 获取台桌列表
const getTables = async () => {
  loading.value = true
  try {
    // 模拟数据
    const mockTables = [
      {
        id: 1,
        table_no: 'T001',
        table_name: '1号台九球桌',
        table_type: 'nine_ball',
        size: '9英尺',
        brand: '星牌',
        area: 'A区',
        floor: 1,
        position: '靠窗位置',
        hourly_rate: 60.00,
        member_rate: 48.00,
        peak_rate: 80.00,
        status: 'available',
        is_premium: false,
        current_user_id: null,
        current_order_id: null,
        occupied_since: null,
        has_lighting: true,
        has_scoreboard: false
      },
      {
        id: 2,
        table_no: 'T002',
        table_name: '2号台斯诺克桌',
        table_type: 'snooker',
        size: '12英尺',
        brand: '星牌',
        area: 'B区',
        floor: 1,
        position: '中央位置',
        hourly_rate: 80.00,
        member_rate: 64.00,
        peak_rate: 100.00,
        status: 'occupied',
        is_premium: true,
        current_user_id: 123,
        current_order_id: 456,
        occupied_since: '2025-08-26T14:30:00',
        has_lighting: true,
        has_scoreboard: true
      }
    ]
    
    tables.value = mockTables
    pagination.value.total = mockTables.length
  } catch (error) {
    console.error('获取台桌列表失败:', error)
    ElMessage.error('获取台桌列表失败')
  } finally {
    loading.value = false
  }
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    table_no: '',
    table_type: '',
    status: ''
  }
  getTables()
}

// 刷新数据
const handleRefresh = () => {
  getTables()
}

// 添加台桌
const handleAdd = () => {
  ElMessage.info('添加台桌功能开发中')
}

// 查看台桌
const viewTable = (table) => {
  ElMessage.info('查看台桌详情功能开发中')
}

// 编辑台桌
const editTable = (table) => {
  ElMessage.info('编辑台桌功能开发中')
}

// 处理台桌操作
const handleTableCommand = async (command) => {
  const { action, row } = command
  
  let confirmText = ''
  let statusText = ''
  let newStatus = ''
  
  switch (action) {
    case 'maintenance':
      confirmText = '设为维护中'
      statusText = '维护中'
      newStatus = 'maintenance'
      break
    case 'enable':
      confirmText = '设为可用'
      statusText = '可用'
      newStatus = 'available'
      break
    case 'disable':
      confirmText = '禁用台桌'
      statusText = '已禁用'
      newStatus = 'disabled'
      break
  }
  
  try {
    await ElMessageBox.confirm(`确定要将台桌${row.table_no}${confirmText}吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    // 模拟API调用
    row.status = newStatus
    if (newStatus === 'maintenance' || newStatus === 'disabled') {
      row.current_user_id = null
      row.current_order_id = null
      row.occupied_since = null
    }
    
    ElMessage.success(`台桌状态已更新为${statusText}`)
  } catch (error) {
    // 用户取消操作
  }
}

// 获取台桌类型文本
const getTableTypeText = (type) => {
  switch (type) {
    case 'nine_ball': return '九球桌'
    case 'snooker': return '斯诺克桌'
    case 'chinese_eight': return '中式八球'
    default: return type
  }
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 'available': return '可用'
    case 'occupied': return '占用中'
    case 'maintenance': return '维护中'
    case 'disabled': return '已禁用'
    default: return status
  }
}

// 获取状态标签类型
const getStatusType = (status) => {
  switch (status) {
    case 'available': return 'success'
    case 'occupied': return 'warning'
    case 'maintenance': return 'info'
    case 'disabled': return 'danger'
    default: return 'info'
  }
}

// 格式化时间
const formatTime = (timeString) => {
  if (!timeString) return '-'
  return new Date(timeString).toLocaleString('zh-CN')
}

onMounted(() => {
  getTables()
})
</script>

<style lang="scss" scoped>
.rate-info {
  font-size: 12px;
  line-height: 1.4;
  
  div {
    margin-bottom: 2px;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
}

.occupied-time {
  font-size: 12px;
  color: #666;
}

.equipment {
  display: flex;
  gap: 4px;
  
  .el-icon {
    font-size: 16px;
    color: #1890ff;
  }
}

.floor-info {
  font-size: 12px;
  color: #666;
}
</style>